<template>
  <div>
    <el-dialog :visible="visible" @close="$emit('isDialog', false)">
      <el-form>
        <el-form-item :label-width="labeWidth" label="真实姓名">
          <el-input v-model="bankinfo.name" class="input" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item :label-width="labeWidth" label="手机号">
          <el-input v-model="bankinfo.phone" class="input" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item :label-width="labeWidth" label="身份证号">
          <el-input v-model="bankinfo.idCard" class="input" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item :label-width="labeWidth" label="银行卡号">
          <el-input v-model="bankinfo.bankCard" class="input" placeholder="请输入银行卡号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('isDialog', false)">取 消</el-button>
        <el-button v-preventReClick v-show="isBind" type="primary" @click="bind">绑 定</el-button>
        <!-- <el-button v-show="!isBind" type="primary" @click="bindTo">换 绑</el-button> -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTeacherDetail, bindCard, getTeacherList, teacherBankCardInfo } from '@/api/teacher'
import { checkPhone, checkIdCard, bankCardCheck } from '@/utils/regUtil'

let tid;
export default {
  name: 'BankView',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    dialogType: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      labeWidth: '120px',
      bankinfo: {
        name: '',
        idCard: '',
        phone: '',
        bankCard: ''
      },
      isBind: true,
      isDisabled: false
    }
  },
  methods: {
    getMsg(e) {
      tid = e
      const that = this
      // 教师列表绑卡
      if (this.dialogType === 'teacher') {
        getTeacherDetail(e).then(res => {
          // console.log('绑卡子组件：', res)
          // if (res.data.bankCardInfo.name) {
          //   that.isBind = false
          // } else {
          //   that.isBind = true
          // }
        })
        // console.log('tid111:', tid)
        getTeacherList({ tid }).then(res => {
          // console.log('教师信息：', res.data.list[0])
          if (parseInt(res.code) === 200) {
            var data = res.data.list[0]
            that.bankinfo.name = data.name
            that.bankinfo.idCard = data.idNo
            that.bankinfo.phone = data.mobilePhone
          }
        })
      }
      // 订单列表绑卡
      if (this.dialogType === 'order') {
        teacherBankCardInfo(e).then(res => {
          // console.log('oreder绑卡子组件：', res.data.id)
          // if (res.data.id) {
          //   that.isBind = false
          // } else {
          //   that.isBind = true
          // }
          if (parseInt(res.code) === 200) {
            var data = res.data.teacher
            that.bankinfo.name = data.name
            that.bankinfo.idCard = data.idNo
            that.bankinfo.phone = data.mobilePhone
          }
        })
      }
    },
    bindTo() {
      this.$message.error('暂不支持！');
    },
    bind() {
      this.isDisabled = true
      const { name, idCard, phone, bankCard } = this.bankinfo
      if (name === '' || name == null) {
        this.$message.error('姓名不能为空！');
      } else if (phone === '' || phone == null) {
        this.$message.error('手机号不能为空！');
      } else if (!checkPhone(phone)) {
        this.$message.error('手机号格式不正确！');
      } else if (idCard === '' || idCard == null) {
        this.$message.error('身份证号不能为空！');
      } else if (!checkIdCard(idCard)) {
        this.$message.error('身份证号格式不正确！');
      } else if (bankCard === '' || bankCard == null) {
        this.$message.error('银行卡号不能为空！');
      } else if (!bankCardCheck(bankCard)) {
        this.$message.error('银行卡号格式不正确！');
      } else {
        const data = {
          name: name,
          idCard: idCard,
          phone: phone,
          bankCard: bankCard,
          tid: tid
        }
        console.log(data)
        bindCard(data).then(res => {
          if (parseInt(res.code) === 200) {
            this.$message.success('绑定成功')
            this.$emit('isDialog', false)
          } else {
            this.$message.error('绑定失败')
          }
        })
      }
    }
  }
}
</script>

<style>

</style>
